#!/bin/bash

# 要求用户输入要杀死的端口号
if [ -z "$1" ]; then
    echo "you must input a port"
    exit 0
fi

# 查询是否有占用该端口的进程并提取进程ID
PID=$(netstat -nlp | grep ":$1" | awk '{print $7}' | awk -F '[ / ]' '{print $1}')

# 找到开杀，找不到退出
if [ "$PID" != "" ]; then
    echo "process ID is:${PID}"
else
    echo "port $1 free"
    exit 0
fi

# 处决进程！杀杀杀
kill -9 "${PID}"

if [ $? == 0 ]; then
    echo "kill $1 success"
else
    echo "kill $1 fail"
fi
